<template>
  <div style="padding: 20px">
    <a-space>
      IP:<a-input style="width: 200px" v-model="ip"></a-input> 点位编号:<a-input
        style="width: 200px"
        v-model="bi"
      ></a-input>
      <a-range-picker
        v-model="dateRange"
        :show-time="{ format: 'HH:mm' }"
        format="YYYY-MM-DD HH:mm:ss"
        :placeholder="['开始时间', '结束时间']"
        @change="onChange"
      />
      <a-button @click="queryHistroyAlarm" style="margin-left: 10px"
        >查询</a-button
      >
      <a-button @click="exportExcel">一键导出报表</a-button>
    </a-space>
    <div>
      <a-layout>
        <a-layout-content>
          <alarm-table
            :alarms="alarms"
            :height="htree"
            :hasQuery="1"
            ref="alarmTable"
            style="margin-top: 20px"
          ></alarm-table>
        </a-layout-content>
      </a-layout>
    </div>
  </div>
</template>
<script>
import alarmTable from "@/components/alarm/alarmTable.vue";
import { rawGet } from "@/services/crud";
import moment from "moment";
moment.defaultFormat = "YYYY-MM-DD HH:mm:ss";
//import { getAlarmHistory } from "@/services/alarmAPI.js";
//import ThingTree from "@/components/data/thingTree/ThingTree.vue";
function getAlarmHistoryEx(tid, aid, index, begin, end, ip, bi) {
  if (!tid) {
    tid = "";
  }
  if (!aid) {
    aid = "";
  }
  if (index == undefined) {
    index = 0;
  }
  if (!begin) {
    begin = "";
  }
  if (!end) {
    end = "";
  }
  if (!ip) {
    ip = "";
  }
  if (!bi) {
    bi = "";
  }
  let url = `api/extend/api/m_smartBoxForPolice?qmap[ctype]=query_alarm&qmap[tid]=${tid}&qmap[aid]=${aid}&qmap[mindex]=${index}&qmap[begintime]=${begin}`;
  url = url + `&qmap[endtime]=${end}&qmap[ip]=${ip}&qmap[bi]=${bi}`;
  return rawGet(url);
}
export default {
  name: "AlarmView",
  components: {
    alarmTable,
  },
  data() {
    return {
      alarms: [],
      dateRange: [],
      ip: "",
      bi: "",
    };
  },
  mounted() {
    this.refresh();
  },
  computed: {
    treeData() {
      return this.$store.state.thingView.stationTree;
    },
    htree() {
      let height = document.body.clientHeight - 210;
      return "min-height:" + height + "px";
    },
    thsytle() {
      let height = document.body.clientHeight - 210;
      return "margin-top: 10px;width:100%;height:" + height + "px";
    },
  },
  methods: {
    refresh() {
      this.dateRange = [
        this.getCurrentDataFront().split("/").join("-"),
        this.getCurrentData().split("/").join("-"),
      ];
      this.$store.dispatch("tmodel/refreshTModels");
      this.$store.dispatch("thingView/refreshAllThings", {});
    },
    getCurrentDataFront() {
      let time = new Date(
        new Date() - 7 * 24 * 3600 * 1000
      )
      let stime = moment(time)
      return stime. format();
    },
    // 结束时间
    getCurrentData() {
      let time = new Date()
      let stime = moment(time)
      return stime. format();
    },
    queryHistroyAlarm({ uidSet }) {
      let _this = this;
      let uid = uidSet ? uidSet : "";
      getAlarmHistoryEx(
        uid,
        "",
        -1,
        moment(this.dateRange[0]).format(),
        moment(this.dateRange[1]).format(),
        this.ip,
        this.bi
      ).then((res) => {
        if (res && res.data && res.data.result) {
          if (Array.isArray(res.data.result)){
            _this.alarms = res.data.result;
          }
          else{
            _this.alarms = []
          }
          
        }
      });
    },
    onThingSelected(selectedKeys, { selected, node }) {
      console.log("selected--------", node);
      if (selected) {
        //let id = selectedKeys[0]
        //this.$store.dispatch("thingView/refreshActiveThing", node.dataRef.uid);
        let uid = node.dataRef.uid;
        console.log("query--------", uid);
        this.queryHistroyAlarm({ uidSet: uid });
      }
    },
    onChange(value, dateString) {
      //this.dateRange = dateString;
      console.log("Selected Time: ", value);
      console.log("Formatted Selected Time: ", dateString);
    },
    exportExcel() {
      this.$refs.alarmTable.exportToExcel();
    },
  },
};
</script>
